package ywh.player
{
	import fl.core.UIComponent;
	import flash.automation.StageCapture;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.GraphicsPathWinding;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.media.StageVideo;
	import org.osmf.net.PortProtocol;
	
	/**
	 * 参与布局的Sprite;
	 * 设置呈现区域的时候，都是先将全局呈现区域转换为自身的父容器的坐标后，设置并存储;
	 * @author ywh
	 */
	public class RelayoutSprite extends Sprite
	{
		protected static var _stage:Stage;
		protected var _parent:DisplayObjectContainer;
		protected var _globalToLocalOffsetPoint:Point;
		protected var _localToGlobalOffsetPoint:Point;
		
		protected var _recMap:Object;
		protected var _active:Boolean;
		
		protected var _defaultRectangle:Rectangle;
		protected var _currentRec:Rectangle;
		protected var _beforeHideRec:Rectangle;
		protected var _bandRender:DisplayObject;
		protected var _bandRenderVisible:Boolean;
		
		protected var _bandVideo:StageVideo;
		protected var _bandVideoMask:Sprite;
		protected var _bandVideoVisible:Boolean;
		
		public function RelayoutSprite()
		{
			//如果是Flash IDE构建则执行;
			//如果是AS构建则不会执行以下代码;
			if (parent)
			{
				addEventListener(Event.ADDED, initDefaultRecByFlashIDE);
			}
			_recMap = {};
		}
		
		private function initDefaultRecByFlashIDE(e:Event):void
		{
			removeEventListener(Event.ADDED, initDefaultRecByFlashIDE);
			_parent = parent;
			_active = true;
			_globalToLocalOffsetPoint = _parent.localToGlobal(new Point(0, 0));
			_localToGlobalOffsetPoint = _parent.globalToLocal(new Point(0, 0));
			_defaultRectangle = getRect(_parent);
			setRectangle(_defaultRectangle, false);
		}
		
		/**
		 * 为其设置显示区域
		 * @param	rec
		 */
		public function setRectangle(rec:Rectangle = null, isGlobal:Boolean = true):void
		{
			
			//将全局转化为本地;
			isGlobal && rec && rec.offsetPoint(_globalToLocalOffsetPoint);
			_currentRec = rec || _defaultRectangle;
			handleSetRectangle(_currentRec);
			_bandRender && handleBandRenderSetRec(_currentRec);
			_bandVideo && handleBandVideoSetRec(_currentRec);
		}
		
		/**
		 * 增加一种布局;
		 * @param	name	布局名称
		 * @param	rec		布局数据
		 */
		public function addLayout(name:String, rec:Rectangle, isGlobal:Boolean = true):void
		{
			isGlobal && rec.offsetPoint(_globalToLocalOffsetPoint);
			
			_recMap[name] = rec;
		}
		
		/**
		 * 改变布局;
		 * @param	name
		 */
		public function changeLayout(name:String):void
		{
			setRectangle(_recMap[name] as Rectangle, false);
		}
		
		/**
		 * 子类需要覆盖此方法,用于调整
		 * @param	rec
		 */
		protected function handleSetRectangle(rec:Rectangle):void
		{
			x = rec.x;
			y = rec.y;
		}
		
		/**
		 * 初始位置，此位置为在Flash IDE中拖放的位置;
		 */
		public function get defaultRecToGlobal():Rectangle
		{
			var tempRec:Rectangle = _defaultRectangle.clone();
			tempRec.offsetPoint(_localToGlobalOffsetPoint);
			return tempRec;
		}
		
		/**
		 * 当前的位置;
		 */
		public function get currentRecToGlobal():Rectangle
		{
			var tempRec:Rectangle = _currentRec.clone();
			tempRec.offsetPoint(_localToGlobalOffsetPoint);
			return tempRec;
		}
		
		//=====作为布局容器的时候需要使用========================================================================================
		/**
		 * 静态方法用于将显示对象横向排序;
		 * @param	targetWidth	目标宽度;
		 * @param	padding	元素间隔，如果元素之间间隔未指定情况下，将使用此值
		 * @param	leftElements 左侧元素，形式可以为[num,sprite,num,sprite,sprite],注意，假如当前元素为显示元素，前一个元素为数字则为两元素的间隔，如果前一个元素为显示元素，则使用间隔;
		 * @param	rightElements 同左侧元素；
		 * @param	scaleDP 中间可以伸缩的元素;
		 */
		public static function horizantalLayout(targetWidth:Number, padding:Number, leftElements:Array, rightElements:Array, scaleDP:DisplayObject):void
		{
			var prevLPos:Number = 0;
			var prevRPos:Number = targetWidth;
			var prevElementIsDP:Boolean = false;
			var element:*;
			var dp:DisplayObject;
			
			while (leftElements.length)
			{
				element = leftElements.shift();
				if (element is DisplayObject)
				{
					dp = element as DisplayObject;
					dp.x = prevLPos + (prevElementIsDP ? padding : 0);
					prevLPos += dp.width;
					prevElementIsDP = true;
				}
				else if (element is Number)
				{
					prevLPos += element;
					prevElementIsDP = false;
				}
			}
			
			scaleDP && (scaleDP.x = prevLPos + (prevElementIsDP ? padding : 0));
			prevElementIsDP = false;
			
			while (rightElements.length)
			{
				element = rightElements.pop();
				if (element is DisplayObject)
				{
					dp = element as DisplayObject;
					prevRPos = dp.x = prevRPos - (prevElementIsDP ? padding : 0) - dp.width;
					prevElementIsDP = true;
				}
				else if (element is Number)
				{
					prevRPos -= element;
					prevElementIsDP = false;
				}
			}
			scaleDP && (scaleDP.width = prevRPos - (prevElementIsDP ? padding : 0) - scaleDP.x);
		}
		
		//====绑定渲染器的做法===================================================================================
		/**
		 * 绑定渲染器;被绑定的渲染器，为组件或者显示对象，当本例发生尺寸变化时，被绑定的组件也会发生变化;
		 */
		public function set bandRender(value:DisplayObject):void
		{
			_bandRender = value;
			if (!_active)
			{
				_active = true;
				_parent = _bandRender.parent;
				_localToGlobalOffsetPoint = _parent.globalToLocal(new Point(0, 0));
				_globalToLocalOffsetPoint = _parent.localToGlobal(new Point(0, 0))
				_defaultRectangle = _bandRender.getRect(_parent);
			}
			else
			{
				handleBandRenderSetRec(_currentRec);
				if (_parent)
				{
					_parent.addChildAt(_bandRender, _parent.getChildIndex(this));
					_parent.removeChild(this);
				}
			}
		
		}
		
		public function get bandRender():DisplayObject
		{
			return _bandRender;
		}
		
		private function handleBandRenderSetRec(rec:Rectangle):void
		{
			if (_bandRender is UIComponent)
			{
				(_bandRender as UIComponent).move(_currentRec.x, _currentRec.y);
				(_bandRender as UIComponent).setSize(_currentRec.width, _currentRec.height);
			}
			else if (_bandRender is DisplayObject)
			{
				_bandRender.x = _currentRec.x;
				_bandRender.y = _currentRec.y;
				_bandRender.width = _currentRec.width;
				_bandRender.height = _currentRec.height;
			}
		}
		
		//=========绑定屏幕视频的时候的做法=====================================
		/**
		 * 绑定屏幕视频;
		 */
		public function set bandVideo(value:StageVideo):void
		{
			_bandVideo = value;
			_bandVideoMask = new Sprite();
			_bandVideoMask.visible = true;
			_parent.addChild(_bandVideoMask);
			for (var i:int = stageRef.numChildren - 1; i > -1; i--)
			{
				stageRef.getChildAt(i).mask = _bandVideoMask;
			}
			_parent.removeChild(this);
			bandVideoVisible = true;
		}
		
		public function get bandVideo():StageVideo
		{
			return _bandVideo;
		}
		
		public function set bandVideoVisible(value:Boolean):void
		{
			if (_bandVideo && _bandVideoVisible != value)
			{
				_bandVideoVisible = value;
				if (_bandVideoVisible)
				{
					setRectangle(_beforeHideRec, false);
					_beforeHideRec = null
				}
				else
				{
					_beforeHideRec = _currentRec;
					setRectangle(new Rectangle(-1000, -1000, 100, 100));
				}
			}
		}
		
		public function get bandVideoVisible():Boolean
		{
			return _bandVideoVisible;
		}
		
		public static function set stageRef(value:Stage):void
		{			
			_stage = value;		
		}
		
		public function get stageRef():Stage
		{
			return _stage;
		}
		
		private function handleBandVideoSetRec(rec:Rectangle):void
		{
			var stageL:Number = _localToGlobalOffsetPoint.x;
			var stageT:Number = _localToGlobalOffsetPoint.y;
			var stageR:Number = stageL + stageRef.stageWidth;
			var stageB:Number = stageT + stageRef.stageHeight;
			var sL:Number = rec.left;
			var sT:Number = rec.top;
			var sR:Number = rec.right;
			var sB:Number = rec.bottom;
			_bandVideoMask.graphics.clear();
			_bandVideoMask.graphics.beginFill(0x333333, 0.5);
			_bandVideoMask.graphics.drawPath(Vector.<int>([1, 2, 2, 2, 1, 2, 2, 2]), Vector.<Number>([stageL, stageT, stageL, stageB, stageR, stageB, stageR, stageT, sL, sT, sR, sT, sR, sB, sL, sB]), GraphicsPathWinding.EVEN_ODD);
			var tempRec:Rectangle = rec.clone();
			tempRec.offsetPoint(_globalToLocalOffsetPoint)
			_bandVideo.viewPort = tempRec;
		}
	
	}

}